﻿using System;
using System.Threading;
using System.Web;

namespace WASM.WebSite.Logic.Logging
{
	public static class Logger
	{
		public static string Write(Exception ex)
		{
			ExceptionErrorEvent exceptionErrorEvent = new ExceptionErrorEvent(ex);
			exceptionErrorEvent.Raise();
			return exceptionErrorEvent.EventID.ToString();
		}


		public static HttpContext HttpContext
		{
			get
			{
				//if current HttpContext is null then check HttpContext in DataSlot of the thread. 
				if (HttpContext.Current != null)
					return HttpContext.Current;
				else
					return (HttpContext)Thread.GetData(Thread.GetNamedDataSlot("httpcontextslot"));
			}
			set
			{
				Thread.SetData(Thread.GetNamedDataSlot("httpcontextslot"), value);
			}
		}

		public static string UserHostAddress
		{
			get
			{
				//if current UserHostAddress is null then check UserHostAddress in DataSlot of the thread. 
				if (HttpContext.Current != null)
				{
					string userHostAddress = null;
					try
					{
						userHostAddress = HttpContext.Current.Request.UserHostAddress;
					}
					catch (ArgumentException ex)
					{
						return String.Format("Error during getting client ip: {0}", ex.Message);
					}
					return userHostAddress;
				}
				else
					return (string)Thread.GetData(Thread.GetNamedDataSlot("userhostaddressslot"));
			}
			set
			{
				Thread.SetData(Thread.GetNamedDataSlot("userhostaddressslot"), value);
			}
		}
	}
}
